﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using DBLibrary;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class BillWebService : System.Web.Services.WebService
{
    [WebMethod]
    public string[] GetCustomerNames(string prefixText, int count)
    {
        List<string> result = new List<string>();
        List<CustomerDetail> list = PoseidonDBHelper.UserDB.Select<CustomerDetail>();
        foreach (CustomerDetail cd in list)
        {
            if (cd.Name.StartsWith(prefixText))
            {
                result.Add(cd.Name);
            }
        }
        return result.ToArray();
    }

    [WebMethod]
    public BillDetail AddNewSpendItemPrice(string customerName, string itemName, string spendItemId, int count)
    {
        DB db = PoseidonDBHelper.UserDB;
        BillDetail bd = new BillDetail();
        bd.CustomerName = customerName;
        bd.SpendItemCount = count;
        bd.SpendItemName = itemName;
        SpendItemDetail si = db.Select<SpendItemDetail>(new DBParam("SpendItemId", spendItemId))[0];
        bd.SpendItemPrice = si.SpendItemPrice;
        bd.SpendTypeName = db.Select<SpendTypeDetail>(new DBParam("SpendTypeId", si.SpendTypeId))[0].SpendTypeName;
        bd.Time = DateTime.Now;
        long id;
        db.Insert(bd, out id);
        bd.BillId = id;
        return bd;
    }

    [WebMethod]
    public long DeleteSpendItem(long id)
    {
        PoseidonDBHelper.UserDB.Delete<SpendItemDetail>(new DBParam("SpendItemId", id));
        return id;
    }

    [WebMethod]
    public bool ValidateCustomerName(string name)
    {
        return PoseidonDBHelper.UserDB.Exist<CustomerDetail>(new DBParam("Name", name));
    }
}
